﻿#region Copyright and License Notice
// Copyright (C)2010-2016 - Rob Levine and other contributors
// http://configgen.codeplex.com
// 
// This file is part of ConfigGen.
// 
// ConfigGen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// ConfigGen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License and 
// the GNU Lesser General Public License along with ConfigGen.  
// If not, see <http://www.gnu.org/licenses/>
#endregion

using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using ConfigGen.Core.TemplateProcessing;
using ConfigGen.Utilities;
using ConfigGen.Utilities.Extensions.System.Reflection;
using NUnit.Framework;

namespace ConfigGen.Core.Tests.EndToEndTests
{
    [TestFixture]
    public class RemoveAttributeFromParentProcessorEndToEndTests
    {
        [Test]
        public void AttributeIsRemovedWhenConditionIsTrue()
        {
            using (var testDirectory = new DisposableDirectory())
            {
                string settingsFile = Path.Combine(testDirectory.FullName, "App.Config.Settings.xls");
                Assembly.GetExecutingAssembly().CopyEmbeddedResourceFileTo("TestResources.RemoveAttributeFromParent.App.Config.Settings.xls", settingsFile);

                string templateFile = Path.Combine(testDirectory.FullName, "App.Config.Template.xml");
                Assembly.GetExecutingAssembly().CopyEmbeddedResourceFileTo("TestResources.RemoveAttributeFromParent.App.Config.Template.xml", templateFile);

                var settings = new Preferences
                {
                    SettingsFile = settingsFile,
                    TemplateFile = templateFile,
                    OutputDirectory = Path.Combine(testDirectory.FullName, "Configs"),
                };

                var results = TestHelper.ExecuteCfgTest(settings);

                TestHelper.CheckExitResults(results, false);

                var fileDevServer = XElement.Load(Path.Combine(testDirectory.FullName, "Configs\\DevServer\\App.Config"));
                Assert.IsFalse(fileDevServer.Descendants(XName.Get("RemoveAttributeFromParent", TemplateProcessor.ConfigGenXmlNamespace)).Any(), "RemoveAttributeFromParent element should have been removed");
                var anElement = fileDevServer.Descendants(XName.Get("AnElement")).FirstOrDefault();
                Assert.IsFalse(anElement.Attributes(XName.Get("attribute1", "")).Any(), "attribute1 should have been removed");
                Assert.IsTrue(anElement.Attributes(XName.Get("attribute3", "")).Any(), "attribute3 should be untouched in all cases");
            }
        }

        [Test]
        public void AttributeIsNotRemovedWhenConditionIsFalse()
        {
            using (var testDirectory = new DisposableDirectory())
            {
                string settingsFile = Path.Combine(testDirectory.FullName, "App.Config.Settings.xls");
                Assembly.GetExecutingAssembly().CopyEmbeddedResourceFileTo("TestResources.RemoveAttributeFromParent.App.Config.Settings.xls", settingsFile);

                string templateFile = Path.Combine(testDirectory.FullName, "App.Config.Template.xml");
                Assembly.GetExecutingAssembly().CopyEmbeddedResourceFileTo("TestResources.RemoveAttributeFromParent.App.Config.Template.xml", templateFile);

                var settings = new Preferences
                {
                    SettingsFile = settingsFile,
                    TemplateFile = templateFile,
                    OutputDirectory = Path.Combine(testDirectory.FullName, "Configs"),
                };

                var results = TestHelper.ExecuteCfgTest(settings);

                TestHelper.CheckExitResults(results, false);

                var fileDevServer = XElement.Load(Path.Combine(testDirectory.FullName, "Configs\\ProdServer\\App.Config"));
                Assert.IsFalse(fileDevServer.Descendants(XName.Get("RemoveAttributeFromParent", TemplateProcessor.ConfigGenXmlNamespace)).Any(), "RemoveAttributeFromParent element should have been removed");
                var anElement = fileDevServer.Descendants(XName.Get("AnElement")).FirstOrDefault();
                Assert.IsTrue(anElement.Attributes(XName.Get("attribute1", "")).Any(), "attribute1 should not have been removed");
                Assert.IsTrue(anElement.Attributes(XName.Get("attribute3", "")).Any(), "attribute3 should be untouched in all cases");
            }
        }

        [Test]
        public void MultipleAttributesRemovedWhenConditionIsTrue()
        {
            using (var testDirectory = new DisposableDirectory())
            {
                string settingsFile = Path.Combine(testDirectory.FullName, "App.Config.Settings.xls");
                Assembly.GetExecutingAssembly().CopyEmbeddedResourceFileTo("TestResources.RemoveAttributeFromParent.App.Config.Settings.xls", settingsFile);

                string templateFile = Path.Combine(testDirectory.FullName, "App.Config.Template.xml");
                Assembly.GetExecutingAssembly().CopyEmbeddedResourceFileTo("TestResources.RemoveAttributeFromParent.App.Config.Template.xml", templateFile);

                var settings = new Preferences
                {
                    SettingsFile = settingsFile,
                    TemplateFile = templateFile,
                    OutputDirectory = Path.Combine(testDirectory.FullName, "Configs"),
                };

                var results = TestHelper.ExecuteCfgTest(settings);

                TestHelper.CheckExitResults(results, false);

                var fileDevServer = XElement.Load(Path.Combine(testDirectory.FullName, "Configs\\DevServer\\App.Config"));
                Assert.IsFalse(fileDevServer.Descendants(XName.Get("RemoveAttributeFromParent", TemplateProcessor.ConfigGenXmlNamespace)).Any(), "RemoveAttributeFromParent element should have been removed");
                var anElement = fileDevServer.Descendants(XName.Get("AnElement")).FirstOrDefault();
                Assert.IsFalse(anElement.Attributes(XName.Get("attribute1", "")).Any(), "attribute1 should have been removed");
                Assert.IsFalse(anElement.Attributes(XName.Get("attribute2", "")).Any(), "attribute2 should also be removed due to second remove instruction");
            }
        }
    }
}
